package com.and9.mchen.mcmusic.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.and9.mchen.mcmusic.dao.IMusicStyleDao;
import com.and9.mchen.mcmusic.entity.MusicStyle;
import com.and9.mchen.mcmusic.exception.DaoException;
import com.and9.mchen.mcmusic.manager.DaoManager;

public class MusicStyleHelper {

	private static Log log=LogFactory.getLog(MusicStyleHelper.class);
	private static IMusicStyleDao musicStyleDao=null;
	private static List<MusicStyle> musicStyles=null;
	
	static{
		initAllMusicStyles();
	}
	
	private static void initAllMusicStyles(){
		
		musicStyleDao=DaoManager.getMusicStyleDao();
		
		try {
			musicStyles=musicStyleDao.getAllMusicStyles();
		} catch (DaoException e) {
			log.error("init music styles failure", e);
			musicStyles=new ArrayList<MusicStyle>();
		}
		musicStyles=Collections.synchronizedList(musicStyles);
	}
	
	public static void reloadMusicStyle(){
		initAllMusicStyles();
	}
	
	public static MusicStyle findMusicStyleByMusicStyleId(long id){
		
		if(!GEntityHelper.isEmpty(musicStyles)){
			
			for(MusicStyle musicStyle : musicStyles){
				if(musicStyle.getId().equals(id)){
					return musicStyle;
				}
			}
		}
		
		return null;
		
	}
	
	public static MusicStyle findmusMusicStyleByMusicStyleName(String name){
		
		if(!GEntityHelper.isEmpty(musicStyles)){
			for(MusicStyle musicStyle : musicStyles){
				if(musicStyle.getStyleName().equals(name)){
					return musicStyle;
				}
			}
		}
		
		return null;
		
	}
	
	public static List<MusicStyle> getAllMusicStyles(){
		return GEntityHelper.isEmpty(musicStyles) ? null : musicStyles;
	}
	
}
